import { Button, message, Popconfirm, Input, Select } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import _ from 'lodash';
import request from '@/utils/request';
import apiPath from '@/utils/apiPath.js';
import { loadScript } from '@/utils/utils.js';
import Vconsole from 'vconsole';
new Vconsole()
const PageContent = () => {
  
  const wechatLogin = async() => {
    var appId = "wxb86549d2232fa48d";
    var redirectUri = encodeURIComponent("http://192.168.101.63:7070/wechatCodeDeal");
    var scope = "snsapi_userinfo";
    let authUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=1#wechat_redirect`;
    let authUrl2 = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=1#wechat_redirect`
    window.location.href = authUrl2;
  }

  const wechatQrLogin = () => {
    var appId = "wxb86549d2232fa48d";
    var redirectUri = encodeURIComponent("http://192.168.101.63:7070/wechatCodeDeal");
    var scope = "snsapi_login";
    // var obj = new WxLogin({
    //   self_redirect:true,
    //   id:"login_container", 
    //   appid: appId, 
    //   scope: scope, 
    //   redirect_uri: redirectUri,
    //   state: Math.ceil(Math.random() * 1000),
    //   style: 'black',
    //   href: ""
    // });
    // 这是网易云音乐的，可以跳转
    // window.location.href = `https://open.weixin.qq.com/connect/qrconnect?appid=wxb86549d2232fa48d&response_type=code&redirect_uri=http://music.163.com&forcelogin=true&scope=snsapi_login&lang=zh_CN#wechat_redirect`;
     window.location.href = `https://open.weixin.qq.com/connect/qrconnect?appid=${appId}&response_type=code&redirect_uri=http://192.168.101.63:7070/wechatCodeDeal&forcelogin=true&scope=snsapi_login&lang=zh_CN#wechat_redirect`;
  }

  const sendTmpMsg = async(code) => {
    const hide = message.loading('正在调用');
    try {
      let res = await request(apiPath.WX_SEND_TMP_MSG, {
        method: 'GET',
        params: {
        },
      });
      hide();
      message.success(`调用成功`);
      return res.data;
    } catch (error) {
      console.error(error);
      hide();
      return false;
    }
  }

  // useEffect(async () => {
  //   await loadScript('https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js');
  // }, [])

  return (
    <PageContainer>
      <Button
        type="primary"
        key="primary"
        onClick={async () => {
          wechatLogin()
          // wechatQrLogin()
        }}
      >
        微信登录
      </Button>
      <Button
        type="primary"
        key="primary"
        onClick={async () => {
          sendTmpMsg()
        }}
      >
        发送模板消息
      </Button>
      {/* <div id="login_container">二维码容器</div> */}
    </PageContainer>
  );
};

export default PageContent;
